package com.piz.web.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.BankDetails;
import com.piz.model.Ordertxn;
import com.piz.model.Payment_Type;
import com.piz.model.Ship_Addr;
import com.piz.service.common.AppCache;
import com.piz.service.model.Cart;
import com.piz.service.model.ConfirmOrderBean;
import com.piz.service.orderservice.PaymentService;


/**
 * @author prameetk.sharma
 *
 */
@Controller
public class ConfirmOrderAction {
	
	/**
	 * 
	 */
	@Autowired
	ConfirmOrderValidation confirmOrderValidation;
	
	/**
	 * 
	 */
	@Autowired
	PaymentService paymentService;
	
	//private final static Log LOGGER = LogFactory.getLog(ConfirmOrderAction.class);
	
	/**
	 * @param bean
	 * @param order_txn
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="confirmorder",method = RequestMethod.GET)
	public ModelAndView confirmOrderEdit(ConfirmOrderBean confirmOrderBean,Ordertxn order_txn,Model model, HttpServletRequest request)
	{
		Cart cart = (Cart) AppCache.getInstance().getCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId());
		List<Payment_Type> paymentTypes = paymentService.fetchAllPaymentMethods();
		List<BankDetails> banks = paymentService.fetchAllBanks();
		if(confirmOrderBean == null)
		{
			confirmOrderBean = new ConfirmOrderBean(new Ship_Addr(), null, null);
			confirmOrderBean.setPayment_type_id(1l);
		}
		else if(confirmOrderBean.getShipAddr() == null)
		{
			confirmOrderBean.setShipAddr(new Ship_Addr());
		}
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		refrenceData.put("paymentTypes", paymentTypes);
		refrenceData.put("banks", banks);
		refrenceData.put("cart", cart);
		refrenceData.put("confirmOrderBean", confirmOrderBean);
		model.addAllAttributes(refrenceData);
		return new ModelAndView("pizza/order-confirm");
	}
	
	/**
	 * @param confirmOrderBean
	 * @param result
	 * @param model
	 * @return
	 */
	@RequestMapping(value="makePayment",method = RequestMethod.POST)
	public String processOrder(@Valid @ModelAttribute ConfirmOrderBean confirmOrderBean,BindingResult result,Map<String, Object> model)
	{
		confirmOrderValidation.validate(confirmOrderBean, result);
		final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
		
		if (result.hasErrors()) 
		{
			List<Payment_Type> paymentTypes = paymentService.fetchAllPaymentMethods();
			List<BankDetails> banks = paymentService.fetchAllBanks();
			Cart cart = (Cart) AppCache.getInstance().getCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId());
			if(confirmOrderBean == null)
			{
				confirmOrderBean = new ConfirmOrderBean(new Ship_Addr(), null, null);
				confirmOrderBean.setPayment_type_id(new Long(1));
			}
			else if(confirmOrderBean.getShipAddr() == null)
			{
				confirmOrderBean.setShipAddr(new Ship_Addr());
			}
			//Map<String, Object> refrenceData = new HashMap<String, Object>();
			model.put("cart", cart);
			model.put("confirmOrderBean", confirmOrderBean);
			model.put("paymentTypes", paymentTypes);
			model.put("banks", banks);
			return "pizza/order-confirm";
		}
		paymentService.saveNewOrder(confirmOrderBean, currentUser);
		AppCache.getInstance().setCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId(), null);
		return "pizza/confirm-payment";
	}
}
